#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include "rtsp-server.h"

extern int VIM_RtspClientInit(int clientId, const char* host, int port, const char* url, int queuenum,int framelen,int istcp);

extern void VIM_RtspClientUninit(int clientId);

extern int VIM_RtspClientGetStream(int client, unsigned char *data,int *len);

extern int VIM_RtspClientRealeaseStream(int clientId, int index);


void *client_recv_stream_func1(void *arg)
{
	printf("enter client_recv_stream_func1\n");
	int recvlen = 0;
	int clientId = 1;
	int count = 0;
	FILE* fp = fopen("./testclentchn1.265","wb" );
	unsigned char *data = (char *)malloc(500*1080);
	int ret = VIM_RtspClientInit(clientId,"10.0.2.13",8554,"rtsp://10.0.2.13:8554/test",100,500*1080,0);
	while(1)
	{
		int index = VIM_RtspClientGetStream(clientId,data,&recvlen);
		//printf(">>>>>>>>>>>>>get stream len is [%d]\n",recvlen);
		if(count < 1000)
		{
			fwrite(data,1,recvlen,fp);
			fflush(fp);
			count++;
		}
		VIM_RtspClientRealeaseStream(clientId,index);
	}
	free(data);
	fclose(fp);
}

void *client_recv_stream_func2(void *arg)
{
	printf("enter client_recv_stream_func2\n");
	int recvlen = 0;
	int clientId = 0;
	int count = 0;
	FILE* fp = fopen("./testclentchn0.265","wb" );
	unsigned char *data = (char *)malloc(500*1080);
	int ret = VIM_RtspClientInit(clientId,"10.0.2.13",8554,"rtsp://10.0.2.13:8554/test",100,500*1080,0);
	while(1)
	{
		int index = VIM_RtspClientGetStream(clientId,data,&recvlen);
		//printf(">>>>>>>>>>>>>get stream len is [%d]\n",recvlen);
		if(count < 1000)
		{
			fwrite(data,1,recvlen,fp);
			fflush(fp);
			count++;
		}
		VIM_RtspClientRealeaseStream(clientId,index);
	}
	free(data);
	fclose(fp);
}

int main()
{
	
	pthread_t pth1;
	pthread_t pth2;
	pthread_create(&pth1,NULL,client_recv_stream_func1,NULL);
	pthread_detach(pth1);
	
	pthread_create(&pth2,NULL,client_recv_stream_func2,NULL);
	pthread_detach(pth2);
	
	while(1){sleep(1);}
	
	
}



